package controller.article;

import domain.User;
import service.article.ArticleService;
import service.article.ArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UpdateNews extends HttpServlet {
    private static final long serialVersionUID = 1L;
    ArticleService articleService = new ArticleServiceImpl();
    public UpdateNews() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String title = request.getParameter("title");
        String abs = request.getParameter("abs");
        int type = Integer.parseInt(request.getParameter("type"));
        int state = Integer.parseInt(request.getParameter("state"));
        String info = request.getParameter("news_content");
        User user=(User)request.getSession().getAttribute("userloginflag");
        String author = user.getName();

        int i = articleService.changeArticle(id,type,title,info,abs,author,state);
        String url = null;
        if (i>0) {
            url = "/admin/page/news/newsAdd.jsp?msg=1&id=" + id;
        } else {
            url = "/admin/page/news/newsAdd.jsp?msg=0&id=" + id;
        }
        response.sendRedirect(request.getContextPath() + url);
    }
}